﻿using UnityEngine;
using System.Collections;

public class SystemMsg : MonoBehaviour {
    bool m_bFadeOut;

    public string m_strMsg;
    public int m_iMsgIndex;

    float m_fBoxBorder;
    float m_fBoxSize_PerWord;

	// Use this for initialization
	void Start () {
        transform.localScale = new Vector3(0.05f, 0.05f);

        m_fBoxBorder = 20;
        m_fBoxSize_PerWord = 27.5f;

        transform.GetChild(0).localScale = new Vector3(m_strMsg.Length * m_fBoxSize_PerWord + m_fBoxBorder, 105);
        transform.localPosition = new Vector3(0, -25 * m_iMsgIndex);
        GetComponent<UILabel>().text = m_strMsg;

        StartCoroutine(DestroyTimer());
	}
	
	// Update is called once per frame
	void Update () {
        if (m_bFadeOut)
        {
            GetComponent<UILabel>().color = new Color(1, 1, 1, GetComponent<UILabel>().color.a - 2 / 255f);

            if (GetComponent<UILabel>().color.a <= 0)
                Destroy(gameObject);
        }
	}

    IEnumerator DestroyTimer()
    {
        float fStart = Time.realtimeSinceStartup;
        while (Time.realtimeSinceStartup < fStart + 3.5f)
        {
            yield return null;
        }
        m_bFadeOut = true;
    }
}
